//@author A0096759E

package testcases;


import static org.junit.Assert.assertEquals;
import logic.CommandDecoder;
import logic.ScheduleDisplay;
import logic.ScheduleRedoer;
import logic.ScheduleSearcher;
import logic.ScheduleSorter;
import logic.ScheduleUndoer;

import org.junit.Test;

import storage.ScheduleEvent;
import storage.Storage;
import storage.UndoRedoStackEntry;

public class ScheduleUndoerTest {

	Storage storage = new Storage("undotest.txt");
	ScheduleUndoer undoer = new ScheduleUndoer(storage);
	ScheduleRedoer redoer = new ScheduleRedoer(storage);
	ScheduleSorter sorter = new ScheduleSorter(storage);
	ScheduleSearcher searcher = new ScheduleSearcher(storage, sorter);
	ScheduleDisplay displayer = new ScheduleDisplay(storage, sorter, searcher);
	CommandDecoder decoder = new CommandDecoder(storage);
	

	private static final String ADD_COMMAND = "add";
	
	private static final String SUCCESS_ALL_EVENTS_CLEARED = "All events have been cleared from your Schedule!";
	private static final String SUCCESS_ADDED_EVENT = "The event \"%s\" has been added successfully to your Schedule!";
	private static final String ERROR_NO_SCHEDULE = "Error! There is no tasks.";
	private static final String SUCCESS_DELETED_EVENT = "The event \"%s\" has been removed successfully from your Schedule!";
	private static final String SUCCESS_MSG_UNDO_CLEAR = "Successfully restored the Schedule!";

	@Test
	public void test() {
		assertEquals(SUCCESS_ALL_EVENTS_CLEARED,storage.clearAllEvents());
		StringBuilder correctSequence = new StringBuilder();
		
		ScheduleEvent eventToAdd1 = new ScheduleEvent(0, "Apple Juice", "20121991");
		ScheduleEvent eventToAdd2 = new ScheduleEvent(1, "Banana Milkshake", "21121991", "1200");
		ScheduleEvent eventToAdd3 = new ScheduleEvent(2, "Carrots Are Nice", "22121991", "1200", "1230");
		ScheduleEvent eventToAdd4 = new ScheduleEvent(0, "Donkeys are short", "23121991");

		correctSequence.append("1. " + eventToAdd1.toString()+"\n\n");
		correctSequence.append("2. " + eventToAdd2.toString()+"\n\n");
		correctSequence.append("3. " + eventToAdd3.toString()+"\n\n");
		correctSequence.append("4. " + eventToAdd4.toString());

		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Banana Milkshake" ),storage.addEvent(eventToAdd2));
		storage.getUndoStack().add(new UndoRedoStackEntry(ADD_COMMAND, eventToAdd2));
		
		assertEquals(String.format(SUCCESS_DELETED_EVENT, "Banana Milkshake" ), undoer.undoLast());
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Banana Milkshake" ),redoer.redoLast());
		
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Donkeys are short" ),storage.addEvent(eventToAdd4));
		storage.getUndoStack().add(new UndoRedoStackEntry(ADD_COMMAND, eventToAdd4));
		
		assertEquals(String.format(SUCCESS_DELETED_EVENT, "Donkeys are short" ), undoer.undoLast());
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Donkeys are short" ),redoer.redoLast());
		
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Apple Juice" ),storage.addEvent(eventToAdd1));
		storage.getUndoStack().add(new UndoRedoStackEntry(ADD_COMMAND, eventToAdd1));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Carrots Are Nice" ),storage.addEvent(eventToAdd3));
		storage.getUndoStack().add(new UndoRedoStackEntry(ADD_COMMAND, eventToAdd3));
		
		assertEquals(String.format(SUCCESS_DELETED_EVENT, "Carrots Are Nice" ), undoer.undoLast());
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Carrots Are Nice" ),redoer.redoLast());
		assertEquals(correctSequence.toString(), decoder.decodeUserInput("display all"));
		
		assertEquals(SUCCESS_ALL_EVENTS_CLEARED, storage.clearAllEvents());
		assertEquals(ERROR_NO_SCHEDULE, decoder.decodeUserInput("display all"));
		
		assertEquals(SUCCESS_MSG_UNDO_CLEAR, undoer.undoLast());
		assertEquals(correctSequence.toString(), decoder.decodeUserInput("display all"));
		
		assertEquals(SUCCESS_ALL_EVENTS_CLEARED, redoer.redoLast());
		assertEquals(ERROR_NO_SCHEDULE, decoder.decodeUserInput("display all"));
	}

}
